<?php
namespace Apps\back\Controllers;
use Apps\Back\Controllers\ControllerAuth;
use Apps\Model\Bin;
use Apps\Model\Area;
use Apps\Model\Transtype;
use Phalcon\Filter as Filter;
use Phalcon\Paginator\Adapter\NativeArray as PaginatorModel;


/*
后台仓位管理(bin)
 */
class BinController extends ControllerAuth{

/*
index页面，查询，分页
$currentPage 当前页
 */
    public function indexAction($currentPage=1,$area_id=0,$big_trans_type_id=0,$trans_type_id=0){
        $areas=Area::find();
        $transtype=Transtype::find('pid=0');
        if($this->request->isPost()){
            $area_id=$this->request->getPost('area_id','int');
            $big_trans_type_id=$this->request->getPost('big_trans_type_id','int');
            $trans_type_id=$this->request->getPost('trans_type_id','int');
        }
        $where=array();
        if($area_id>0) $where[]="area_id='$area_id'";
        if($big_trans_type_id>0) $where[]="big_trans_type_id='$big_trans_type_id'";
        if($trans_type_id>0) $where[]="trans_type_id='$trans_type_id'";  
        $bins=Bin::find(array(
            implode(' and ', $where)
            ));
        if(!empty($bins->toArray())){
            foreach ($bins as $value){
                $bin=$value->toArray();
                $bin['area_name']= $value->getArea()->name;
                $bin['trans_name']=$value->getTranstype()->name;
                $trans=Transtype::findFirst('id='.$value->big_trans_type_id);
                $bin['big_trans_name']=$trans->name;
                $ret[]=(object)$bin;
            }
        }else{
            $ret=array();
        }
        $paginator=new PaginatorModel(
                array(
                    "data" =>$ret,
                    "limit" =>5,
                    "page"  =>$currentPage
                    )
            );
        $page=$paginator->getPaginate();    
        $this->view->setVar('area_id',$area_id);
        $this->view->setVar('big_trans_type_id',$big_trans_type_id);
        $this->view->setVar('trans_type_id',$trans_type_id);
        $this->view->setVar('page',$page);
        $this->view->setVar('areas',$areas);
        $this->view->setVar('transtype',$transtype); 
    }

    public function addAction(){
    	$area=Area::find()->toArray();
        $trans=Transtype::find("pid=0")->toArray();
        $this->view->setVar('area',$area);
        $this->view->setVar('trans',$trans);
        if($this ->request ->isPost()){
            $this->view->disable(); 
            $area_id           = $this->request->getPost("area_id", "int");
            $big_trans_type_id = $this->request->getPost("big_trans_type_id", "int");
            $trans_type_id     = $this->request->getPost("trans_type_id", "int");
            $weight            = $this->request->getPost("weight", "float");
            $num               = $this->request->getPost("num","int");  
            $price             = $this->request->getPost("price", "float");
            $lastdate          = $this->request->getPost("lastdate");
            $info              = $this->request->getPost("info","string");

            if($area_id>0 && $big_trans_type_id>0){
                /*
                字符串过滤
                $filter=new Filter();
                $transport_style=$filter->sanitize($transport_style,"trim");
                $transport_type=$filter->sanitize($transport_type,"trim");
                */
                $model=new Bin();
                $model->area_id           = $area_id;
                $model->big_trans_type_id = $big_trans_type_id;
                $model->trans_type_id     = $trans_type_id;
                $model->weight            = $weight;
                $model->num               = $num;
                $model->price             = $price;
                $model->lastdate          = $lastdate;
                $model->info              = $info;

                if(false==$model ->save()){
                    $this->jump('admin/Bin/add', "添加失败");
                    return;
                }else{
                   $this->jump('admin/Bin/index', "添加成功"); 
                } 
            }else{
                $this->jump('admin/Bin/add', "资料不完整请重新填写");
                return;
            }
        }    
    }

    /*
    ajax 回调函数
    @para $id：接收id
     */
    public function getTranstypeAction($id){
        //如何在调试过程中，看到接收到的数据
        //file_put_contents("e:/mylog.log",$id."\r\n",FILE_APPEND);
        $this->view->disable();
        $ret=array();
        if($id>0) $trans=Transtype::find("pid='$id'");
        if($trans) $ret=$trans->toArray();
        echo json_encode($ret);
        return;
    }

    public function editAction($id){
    	$ret= Bin::findFirst("id='$id'");
        $row=$ret->toArray();
        $tran=Transtype::findFirst('id='.$row['big_trans_type_id']);
        $row['big_trans_name']=$tran->name;
        $row['trans_name']=$ret->getTranstype()->name;
        $a=Area::findFirst('id='.$row['area_id']);
        $row['area_name']=$ret->getArea()->name;
        $bin=(object)$row;
    	$this ->view->setVar('bin',$bin);
        //查询地区表，供视图使用
        $area=Area::find()->toArray();
        $this->view->setVar('area',$area);
        //查询运输表大类，供视图使用
        $trans=Transtype::find("pid=0")->toArray();
        $this->view->setVar('trans',$trans);
    }

    public function editokAction($id){
        if($this ->request ->isPost()){
            $this->view->disable(); 
            $area_id              = $this->request->getPost("area_id", "int");
            $big_trans_type_id    = $this->request->getPost("big_trans_type_id", "int");
            $trans_type_id        = $this->request->getPost("trans_type_id", "int");
            $weight               = $this->request->getPost("weight", "float");
            $num                  = $this->request->getPost("num", "int");
            $price                = $this->request->getPost("price", "float");
            $lastdate             = $this->request->getPost("lastdate");
            $info                 = $this->request->getPost("info","string");

            if(!empty($weight) && !empty($num) && !empty($price) && !empty($lastdate)){
                $model=Bin::findFirst("id='$id'");
                //字符串过滤
                //$filter=new Filter();
                //$transport_style=$filter->sanitize($transport_style,"trim");
                //$transport_type=$filter->sanitize($transport_type,"trim");
                $model->area_id             = $area_id;
                $model->big_trans_type_id   = $big_trans_type_id;
                $model->trans_type_id       = $trans_type_id;
                $model->weight              = $weight;
                $model->num                 = $num;
                $model->price               = $price;
                $model->lastdate            = $lastdate;
                $model->info                = $info;

                $model ->save();

            }else{
                $this->jump("admin/Bin/edit/$id", "资料不完整请重新填写");
                return;
            }
        }
        $this->jump('admin/Bin/index', "修改成功");
    }

    public function delAction($id,$currentPage,$area_id,$big_trans_type_id,$trans_type_id){
    	if($id >0){
    		$bin= Bin::findFirst("id='$id'");
    		$bin ->delete();
    	}
    	$this->response->redirect("admin/Bin/index/$currentPage/$area_id/$big_trans_type_id/$trans_type_id");
    }

    public function delallAction($currentPage,$area_id,$big_trans_type_id,$trans_type_id){
        $this->view->disable();
        $ids=$this->request->getPost('ids',"int");
        foreach ($ids as $id) {
            $bin=Bin::findFirst("id='$id'");
            $bin->delete();
        }
        $this->response->redirect("admin/Bin/index/$currentPage/$area_id/$big_trans_type_id/$trans_type_id"); 
    }

}